from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains


class Base():
    def __init__(self, driver):
        self.driver = driver

    # 打开网址
    def open_url(self, url):
        self.driver.get(url)

    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc).is_displayed())
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)

    # # 等待元素出现在DOM中
    # def find_element(self, loc):
    #     WebDriverWait(self.driver, 20, 0.5).until(EC.presence_of_element_located(loc))
    #     return self.driver.findelement(*loc)
    #
    # # 等待元素可见
    # def find_element2(self, loc):
    #     WebDriverWait(self.driver, 20, 0.5).until(EC.visibility_of_element_located(*loc))
    #     return self.driver.findelement(*loc)
    #
    # # 等待元素可点击
    # def find_element3(self, loc):
    #     WebDriverWait(self.driver, 20, 0.5).until(EC.element_to_be_clickable(*loc))
    #     return self.driver.findelement(*loc)

    # 点击
    def click(self, loc):
        self.find_element(loc).click()

    # 鼠标悬停
    def perfrom(self, loc):
        ActionChains(self.driver).move_to_element(loc).perform()

    # 执行 JavaScript 脚本
    def hua_ent(self):
        self.driver.execute_script('window.scrollTo(0,document.boby.scrollHeight)')

    # 输入
    def send_keys(self, loc, test):
        self.find_element(loc).send_keys(test)

    def clear(self, loc):
        cle = self.find_element(loc)
        cle.clear()
